export const SERVICEID = "0000FEE7-0000-1000-8000-00805F9B34FB";
export const NOTIFYID = "0000FEC1-0000-1000-8000-00805F9B34FB";
export const WRITEID = "0000FEC1-0000-1000-8000-00805F9B34FB";

export const BLUETOOTH_MESSAGE = {
	"-1":"已经连接",
	"0":"正常",
	"10000":"未初始化蓝牙适配器",
	"10001":"当前蓝牙适配器不可用",
	"10002":"没有找到指定设备",
	"10003":"连接失败",
	"10004":"没有找到指定服务",
	"10005":"没有找到指定特征值",
	"10006":"当前连接已断开",
	"10007":"当前特征值不支持此操作",
	"10008":"其余所有系统上报的异常",
	"10009":"Android 系统特有，系统版本低于 4.3 不支持 BLE",
	"10012":"连接超时",
	"10013":"连接 deviceId 为空或者是格式不正确"
}


export let filterDevices = (devices,currentBike) => {
	let filterDevices = devices.filter((item) => {
		let name = item.name;
		return name == currentBike.ecuId;
	});
	return filterDevices;
}


export let arrayBufferToHexString = (buffer) => {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function(bit) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('').toUpperCase();
}

export let hexStringToArrayBuffer = (str) => {
	//十六进制转ArrayBuffer
	return new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
		return parseInt(h, 16)
	})).buffer
}

//buffer分包
export let getBufferArrayBy20 = (data) => {
	let num = 0;
	if (typeof data == "undefined") return [];
	let allBuffer = hexStringToArrayBuffer(data);
	let bufferLen = allBuffer.byteLength;
	let bufferArray = [];
	while (bufferLen > 0) {
		let buffer;
		if (bufferLen > 20) {
			buffer = allBuffer.slice(num, num + 20);
			num = num + 20;
			bufferLen -= 20;
			bufferArray.push(buffer);
		} else {
			buffer = allBuffer.slice(num, num + bufferLen);
			num += bufferLen;
			bufferLen -= bufferLen;
			bufferArray.push(buffer);
		}
	}
	return bufferArray;
}


export let sleep = (time) => {
	var startTime = new Date().getTime() + parseInt(time, 10);
	while (new Date().getTime() < startTime) {}
}
